package com.caseProject.puremvc.mediator
{
	import com.caseProject.puremvc.mediator.view.MainView;
	import com.caseProject.puremvc.mediator.view.MenuActivityView;
	import com.caseProject.puremvc.mediator.view.MenuIdleToolsView;
	import com.caseProject.puremvc.mediator.view.MenuStateView;
	import com.caseProject.puremvc.model.Diagram;
	import com.caseProject.puremvc.model.DiagramNames;
	import com.caseProject.puremvc.notifications.NotificationNames;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class MainMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "MainStageMediator";
		
		public function MainMediator(viewComponent:MainView)
		{
			super(NAME, viewComponent);
		}
		
		public function get mainStageView ():MainView
		{
			return viewComponent as MainView;
		}
		
		override public function listNotificationInterests():Array
		{
			return [
					NotificationNames.NEW_DIAGRAM_STATE,
					NotificationNames.NEW_DIAGRAM_ACTIVITY,
					NotificationNames.MENU_LIST_OPEN_DIAGRAM,
					NotificationNames.OPEN_PROJECT_SUCESS,
					NotificationNames.CREATE_PROJECT_SUCESS,
					NotificationNames.CLOSE_PROJECT_SUCESS
					];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case NotificationNames.CLOSE_PROJECT_SUCESS:					
					this.mainStageView.changeMenuDiagram(MenuIdleToolsView);
					break;
				
				case NotificationNames.CREATE_PROJECT_SUCESS:					
					this.mainStageView.changeMenuDiagram(MenuIdleToolsView);
					break;
				
				case NotificationNames.OPEN_PROJECT_SUCESS:					
					this.mainStageView.changeMenuDiagram(MenuIdleToolsView);
					break;
				
				case NotificationNames.NEW_DIAGRAM_STATE:					
					this.mainStageView.changeMenuDiagram(MenuStateView);
					this.facade.registerMediator(new MenuStateMediator (this.mainStageView.diagramMenu as MenuStateView));
					break;
				
				case NotificationNames.NEW_DIAGRAM_ACTIVITY:
					this.mainStageView.changeMenuDiagram(MenuActivityView);
					this.facade.registerMediator(new MenuActivityMediator(this.mainStageView.diagramMenu as MenuActivityView));
					break;
				
				case NotificationNames.MENU_LIST_OPEN_DIAGRAM:
					var diagramVO:Diagram = notification.getBody() as Diagram;
					var type:String = diagramVO.type;
					
					if (type == DiagramNames.STATE)
					{
						this.mainStageView.changeMenuDiagram(MenuStateView);
						this.facade.registerMediator(new MenuStateMediator (this.mainStageView.diagramMenu as MenuStateView));
					} 
					else if (type == DiagramNames.ACTIVITY) 
					{
						this.mainStageView.changeMenuDiagram(MenuActivityView);
						this.facade.registerMediator(new MenuActivityMediator(this.mainStageView.diagramMenu as MenuActivityView));
					}					
					break;
			}
		}
	}
}